package cn.smbms.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.smbms.bean.model.Staff;
import cn.smbms.bean.vo.StaffVo;
import cn.smbms.biz.IStaffBiz;

	@Controller
	@RequestMapping("/staff")
	public class StaffController {
		@Resource(name = "staffBiz")
		private IStaffBiz biz;
		List<Staff> list = null;
	
	@RequestMapping("/get")
	public String get(HttpServletRequest request,StaffVo staffVo,Model model){	//获取所有用户信息
		try {
			list = biz.getStaffByStaffVo(staffVo);
			int totalPages = biz.getTotalPages(staffVo).getTotalPages();
			model.addAttribute("list",list);
			model.addAttribute("staffVo",staffVo);
			model.addAttribute("totalPages",totalPages);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "staff/index";
	}

	
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="staffId")Integer staffId,HttpServletRequest request,Model model) throws UnsupportedEncodingException{	//删除用户信息
		request.setCharacterEncoding("utf-8");
		int result = biz.delete(staffId);
		if(result > 0) {
			model.addAttribute("msg", "删除成功！");
		}else{
			model.addAttribute("msg", "删除失败！");
		}
		return "redirect:/staff/get";
	}

	
	@RequestMapping("/addPage")
	public String addPage(){	//进入添加用户页面
		return "staff/addStaff";
	}
	
	
	@RequestMapping("/add")
	public String add(HttpServletRequest request,Model model) throws UnsupportedEncodingException{	//执行添加用户信息操作
		request.setCharacterEncoding("utf-8");
		String staffNum = request.getParameter("staffNum");//用户登录账号
		String chnName = request.getParameter("chnName");//员工中文姓名
		String surName = request.getParameter("surName");//员工英文全名
		String pwd = request.getParameter("pwd");//密码
		String mobile = request.getParameter("mobile");//手机号码
		Staff staff = new Staff();
		staff.setStaffNum(staffNum);
		staff.setChnName(chnName);
		staff.setSurName(surName);
		staff.setPwd(pwd);
		staff.setMobile(mobile);
		if(biz.findStaffByStaffNum(staffNum) == null){
			int result = biz.add(staff);
			if(result > 0){
				model.addAttribute("msg", "添加成功！");
				return "redirect:/staff/get";
			}else{
				model.addAttribute("msg", "添加失败！");
				return "staff/addStaff";
			}
		}else{
			model.addAttribute("msg","该登陆帐号已存在！");
			return "staff/addStaff";
		}
	}
	
	@RequestMapping("/modifyPage")
	public String modifyPage(@RequestParam(value="staffNum")String staffNum,Model model){	//进入修改用户信息页面
		Staff staff = biz.findStaffByStaffNum(staffNum);
		model.addAttribute("staff",staff);
		return "staff/modifyStaff";
	}
	
	@RequestMapping("/update")
	public String update(HttpServletRequest request,Model model) throws UnsupportedEncodingException{	//修改用户信息
		request.setCharacterEncoding("utf-8");
		Staff staff = biz.findStaffByStaffNum(request.getParameter("staffNum"));
		String chnName = request.getParameter("chnName");//员工中文姓名
		String surName = request.getParameter("surName");//员工英文全名
		String pwd = request.getParameter("pwd");//密码
		String mobile = request.getParameter("mobile");//手机号码
		int changePwd = Integer.parseInt(request.getParameter("changePwd"));//是否修改密码标记
		//int deleteFlag = Integer.parseInt(request.getParameter("deleteFlag"));//当前用户状态（是否删除）
		String loginIP = request.getParameter("loginIP");//最近登录IP
		int loginStatus = Integer.parseInt(request.getParameter("loginStatus"));//登录状态
		staff.setChnName(chnName);
		staff.setSurName(surName);
		staff.setPwd(pwd);
		staff.setMobile(mobile);
		staff.setChangePwd(changePwd);
		//staff.setDeleteFlag(deleteFlag);
		staff.setLoginIP(loginIP);
		staff.setLoginStatus(loginStatus);
		int result = biz.update(staff);
		if(result > 0){
			model.addAttribute("msg", "修改成功！");
			return "redirect:/staff/get";
		}else{
			model.addAttribute("msg","修改失败！");
			return "staff/modifyStaff";
		}
	}
}